#include "qtreeindexdata.h"

#include "xmlobject.h"

QTreeIndexData::QTreeIndexData()
{

}

QTreeIndexData::QTreeIndexData(const QTreeIndexData &data)
{
    m_attributes=data.m_attributes;
}

void QTreeIndexData::from_xml(XMLObject *xml)
{
    m_attributes=xml->get_propertys();
}

void QTreeIndexData::to_xml(XMLObject *xml)
{
    xml->set_title("index");
    QMapIterator<QString,QString> it(m_attributes);
    while(it.hasNext())
    {
        it.next();
        xml->set_property(it.key(),it.value());
    }
}

bool QTreeIndexData::operator ==(const QTreeIndexData &data)
{
    return m_attributes==data.m_attributes;
}

bool QTreeIndexData::operator !=(const QTreeIndexData &data)
{
    return !(*this==data);
}

QTreeRowData::QTreeRowData()
{

}

QTreeRowData::QTreeRowData(const QTreeRowData &data)
{
    foreach(QTreeIndexData* d,data.m_indexs)
    {
        m_indexs.append(new QTreeIndexData(*d));
    }

    foreach(QTreeRowData *r,data.m_children)
    {
        m_children.append(new QTreeRowData(*r));
    }
}

QTreeRowData::~QTreeRowData()
{
    qDeleteAll(m_indexs);
    m_indexs.clear();
    qDeleteAll(m_children);
    m_children.clear();
}

void QTreeRowData::from_xml(XMLObject *xml)
{
    QList<XMLObject*> list=xml->get_children();

    qDeleteAll(m_indexs);
    m_indexs.clear();
    qDeleteAll(m_children);
    m_children.clear();

    foreach(XMLObject *o,list)
    {
        if(o->get_title()=="index")
        {
            QTreeIndexData *index=new QTreeIndexData;
            index->from_xml(o);
            m_indexs.append(index);
        }
        else if(o->get_title()=="row")
        {
            QTreeRowData *row=new QTreeRowData;
            row->from_xml(o);
            m_children.append(row);
        }
    }
}

void QTreeRowData::to_xml(XMLObject *xml)
{
    xml->set_title("row");

    foreach(QTreeIndexData *index,m_indexs)
    {
        XMLObject *o=new XMLObject(xml);
        index->to_xml(o);
    }

    foreach(QTreeRowData* c,m_children)
    {
        XMLObject *o=new XMLObject(xml);
        c->to_xml(o);
    }
}

bool QTreeRowData::operator ==(const QTreeRowData &data)
{
    if(m_indexs.size()!=data.m_indexs.size() || m_children.size()!=data.m_children.size())
    {
        return false;
    }

    int c=m_indexs.size();
    for(int i=0;i<c;i++)
    {
        if(*m_indexs.at(i)!=*data.m_indexs.at(i))
        {
            return false;
        }
    }

    c=m_children.size();

    for(int i=0;i<c;i++)
    {
        if(*m_children.at(i)!=*data.m_children.at(i))
        {
            return false;
        }
    }
    return true;
}

bool QTreeRowData::operator !=(const QTreeRowData &data)
{
    return !(*this==data);
}

QTreeData::QTreeData()
{

}

QTreeData::QTreeData(const QTreeData &data)
{
    m_labels=data.m_labels;

    foreach(QTreeRowData *r,data.m_rows)
    {
        m_rows.append(new QTreeRowData(*r));
    }
}

QTreeData::~QTreeData()
{
    qDeleteAll(m_rows);
    m_rows.clear();
}

void QTreeData::from_xml(XMLObject *xml)
{
    m_labels.clear();
    qDeleteAll(m_rows);
    m_rows.clear();

    QList<XMLObject*> list=xml->get_children();

    foreach(XMLObject *o,list)
    {
        if(o->get_title()=="label")
        {
            m_labels.append(o->text());
        }
        else if(o->get_title()=="row")
        {
            QTreeRowData *row=new QTreeRowData;
            row->from_xml(o);
            m_rows.append(row);
        }
    }
}

void QTreeData::to_xml(XMLObject *xml)
{
    foreach(const QString &str,m_labels)
    {
        XMLObject *o=new XMLObject(xml);
        o->set_title("label");
        o->set_text(str);
    }

    foreach(QTreeRowData* row,m_rows)
    {
        XMLObject *o=new XMLObject(xml);
        row->to_xml(o);
    }
}

bool QTreeData::operator ==(const QTreeData &data)
{
    if(m_labels!=data.m_labels)
    {
        return false;
    }

    if(m_rows.size()!=data.m_rows.size())
    {
        return false;
    }

    int c=m_rows.size();

    for(int i=0;i<c;i++)
    {
        if(*m_rows.at(i)!=*data.m_rows.at(i))
        {
            return false;
        }
    }
    return true;
}


bool QTreeData::operator !=(const QTreeData &data)
{
    return !(*this==data);
}
